<template>
    <div id="newsList">
        <!-- 页面头部 -->
        <HomePageHead></HomePageHead>

        <!-- 导航栏 -->
        <HomePageNavigation1></HomePageNavigation1>

        <!-- 列表页广告一 -->
        <HomeTopTen :imgurl="adList[0]" v-if="adList[0]"></HomeTopTen>

        <!-- 面包屑导航 -->
        <div class="breadcrumb">
            <div class="inner">
                <span class="location">当前位置:</span>
                <el-breadcrumb :separator-icon="ArrowRight">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item>{{ name }}</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
        </div>
        <!-- 资讯列表 -->
        <div class="newsList">
            <div class="inner">
                <div class="innerLeft">
                    <ul class="list">
                        <li v-for="(item, index) in newsList" :key="index">
                            <NuxtLink :to="item.linkurl" target="_blank" v-show="item.islink==1">{{ item.title }}</NuxtLink>
                            <NuxtLink :to="`/newsDetail/${item.id}`" target="_blank" v-if="item.islink==0">{{ item.title }}</NuxtLink>
                        </li>
                    </ul>
                    <!-- 分页器 -->
                    <div class="pagination" v-if="total">
                        <el-pagination size="small" background layout="prev, pager, next" :total="total" class="mt-4"
                            v-model:page-size="pageSize" prev-text="上一页" next-text="下一页" @change="changePage" />
                    </div>
                </div>
                <div class="innerRight">
                    <DetailHotNews></DetailHotNews>
                    <DetailHotNews2></DetailHotNews2>
                </div>
            </div>
        </div>

        <!-- 列表页广告二 -->
        <HomeTopTen :imgurl="adList[1]" v-if="adList[1]"></HomeTopTen>

        <!-- 页面底部 -->
        <HomeFoot1></HomeFoot1>
    </div>
</template>

<script setup>
//1.页面必备依赖 start ---------------------------------------->
import { ElBreadcrumb, ElBreadcrumbItem, ElPagination } from 'element-plus'
import { ArrowRight } from '@element-plus/icons-vue'
//获得跳转过来的id
const route = useRoute();
const articleId = route.params.id; //获得该页面的id
const category_id = route.query.catid; //获得该页面的id
//当前列表名称
const name = ref('')
//1.页面必备依赖 end ---------------------------------------->


//2.页面数据 start ---------------------------------------->
let id = useState("id", () => "")
let total = useState("total", () => 0)
let page = useState("page", () => 1)
let pageSize = useState("pageSize", () => 20)

id.value = route.query.id;

//2.1 获得所有广告
let adList = ref([]);
const {data:adData,error:adError} = requestData('/web/getWebsiteAdvertisement',{method:'GET',query:{'ad_tag':'LIST'}});
if (adError.value) {
    console.error('广告列表请求失败:', adError.value);
} else {
    //当有值了以后再放进去，万恶之源，也是nuxt2和3都存在的一个问题，也许nuxt4会解决这个问题
    if (adData.value && adData.value.data) {
        adList.value = adData.value.data;
    }
}

//2.2新闻列表
const newsList = ref([]);
let newslists = async () => {
    const listData =  await requestDataPromise('/web/getWebsiteArticleList', {
        method: 'GET',
        query: {
            'page': page.value,
            'pageSize': pageSize.value,
            'catid':articleId
        },
    });
    newsList.value = listData.data.rows;
    total.value = listData.data.count;
}
//获得列表
newslists();

//分页事件
let changePage = (value) => {
    console.log("当前页码", value);
    page.value = value
    console.log(page.value);
    newslists()
}

//2.3获得页面名称
let getPageName = async () => {
    const pageName =  await requestDataPromise('/web/getOneWebsiteCategory', {
        method: 'GET',
        query: {
            'catid': articleId
        },
    });

    name.value = pageName.data.name
}
//获得列表
getPageName();

//2.页面数据 end ---------------------------------------->

//4.设置seo信息 start---------------------------------------->
//4.1 设置seo信息
const setData =  await requestDataPromise('/web/getWebsiteCategoryHead', {
    method: 'GET',
    query: {
        'catid': articleId
    },
});

let seoTitle = setData.data.seo_title;
let seoDescription = setData.data.seo_description;
let seoKeywords = setData.data.seo_keywords;

useSeoMeta({
    title: seoTitle,
    meta: [
        { name: 'description', content: seoDescription },
        { name: 'keywords', content: seoKeywords }
    ]
});
//4.设置seo信息 end---------------------------------------->
</script>

<style lang="less" scoped>
@import url('@/assets/css/list.less');
</style>